package Offer;

/**
 * 1～n 整数中 1 出现的次数
 * @author 是阿秋啊
 * @date 2022/08/26 19:58
 **/
public class Num43 {
    public int countDigitOne(int n) {
        int delight = 1,ret = 0;
        int high = n / 10,low = 0,cur = n % 10;
        while(high != 0 || cur != 0){
            if(cur == 0) ret += (high * delight);
            else if(cur == 1) ret += high * delight + low + 1;
            else ret += (high + 1) * delight;
            low += cur * delight;
            cur = high % 10;
            high = high / 10;
            delight*=10;
        }
        return ret;
    }
}